package com.weishu.housing.service.impl;


import com.weishu.common.core.utils.StringUtils;
import com.weishu.common.core.web.domain.AjaxResult;
import com.weishu.core.entity.RoomInfoEntity;
import com.weishu.core.service.ITbRoomInfoService;
import com.weishu.housing.es.FangyuanEsDto;
import com.weishu.housing.mapper.FangyuanEsMapper;
import com.weishu.housing.service.FangyuanEs;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author ny
 * Es服务实现
 */

@Service
public class FangyuanEsImpl implements FangyuanEs {

    @Autowired
    private FangyuanEsMapper fangyuanEsMapper;

    @Autowired
    private ITbRoomInfoService iTbRoomInfoService;

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    /**
     * Es加载房源表
     * @return boolean
     */
    @Override
    public boolean FangyuanGetEsAll(){
        List<RoomInfoEntity> roomInfoEntities = iTbRoomInfoService.queryListroom(new RoomInfoEntity());
        System.out.println(roomInfoEntities.size());
        try {
            roomInfoEntities.forEach(room ->{

                FangyuanEsDto fangyuanEsDto = new FangyuanEsDto();
                fangyuanEsDto.setFrontCover(room.getFrontCover());
                fangyuanEsDto.setId(room.getId());
                fangyuanEsDto.setBusinessId(room.getBusinessId());
                fangyuanEsDto.setHouseId(room.getHouseId());
                fangyuanEsDto.setRoomName(room.getRoomName());
                fangyuanEsDto.setBuildingName(room.getBuildingName());
                fangyuanEsDto.setRoomCode(room.getRoomCode());
                fangyuanEsDto.setProportion(room.getProportion());
                fangyuanEsDto.setDirection(room.getDirection());
                fangyuanEsDto.setRental(room.getRental());
                fangyuanEsDto.setDecorateStyle(room.getDecorateStyle());
                fangyuanEsDto.setFloor(room.getFloor());
                fangyuanEsDto.setFloorCount(room.getFloorCount());
                fangyuanEsDto.setStatus(room.getStatus());
                fangyuanEsDto.setRoomStatus(room.getRoomStatus());
                fangyuanEsDto.setPrivateFacility(room.getPrivateFacility());
                fangyuanEsDto.setPrivateFacilityList(room.getPrivateFacilityList());
                fangyuanEsDto.setUrl(room.getUrl());
                fangyuanEsDto.setCreateBy(room.getCreateBy());
                fangyuanEsDto.setName(room.getName());
                fangyuanEsDto.setPublishStatus(room.getPublishStatus());
                fangyuanEsDto.setPublishStatusName(room.getPublishStatusName());

                fangyuanEsMapper.save(fangyuanEsDto);
            });
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    /**
     * Es条件查询
     * @return AjaxResult
     */
    @Override
    public AjaxResult fangyuanquery(RoomInfoEntity roomInfoEntity, Integer pageNo, Integer pageSize) {
        BoolQueryBuilder mustQuery = QueryBuilders.boolQuery();
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
        //判断实体类是否为空
        if(Objects.nonNull(roomInfoEntity)){

            //判断出租状态
            if(Objects.nonNull(roomInfoEntity.getStatus())){
                mustQuery.must(QueryBuilders.termQuery("status",roomInfoEntity.getStatus()));
                //nativeSearchQueryBuilder.withQuery(QueryBuilders.termQuery("status",roomInfoEntity.getStatus()));
            }
            //判断发布状态
            if(Objects.nonNull(roomInfoEntity.getPublishStatus())){
               // nativeSearchQueryBuilder.withQuery(QueryBuilders.termQuery("publishStatus",roomInfoEntity.getPublishStatus()));
                mustQuery.must(QueryBuilders.termQuery("publishStatus",roomInfoEntity.getPublishStatus()));
            }
            //判断楼盘是否为空
            if(!StringUtils.isEmpty(roomInfoEntity.getName())){
                //nativeSearchQueryBuilder.withQuery(QueryBuilders.matchQuery("name",roomInfoEntity.getName()));
                mustQuery.must(QueryBuilders.matchQuery("name",roomInfoEntity.getName()));
            }
            //判断房源是否为空
            if(!StringUtils.isEmpty(roomInfoEntity.getRoomName())){
                //nativeSearchQueryBuilder.withQuery(QueryBuilders.matchQuery("roomName",roomInfoEntity.getRoomName()));
                mustQuery.must(QueryBuilders.matchQuery("roomName",roomInfoEntity.getRoomName()));
            }

        }
        nativeSearchQueryBuilder.withQuery(mustQuery);
        PageRequest of = PageRequest.of(pageNo-1, pageSize);
        NativeSearchQuery build = nativeSearchQueryBuilder.withPageable(of).build();

        SearchHits<FangyuanEsDto> search = elasticsearchRestTemplate.search(build, FangyuanEsDto.class);

        Stream<SearchHit<FangyuanEsDto>> searchHitStream = search.get();
        List<SearchHit<FangyuanEsDto>> collect = searchHitStream.collect(Collectors.toList());

        List<FangyuanEsDto> collect1 = collect.stream().map(co -> co.getContent()).collect(Collectors.toList());

        collect1.forEach(c-> System.out.println(c.toString()));

        return new AjaxResult(200,search.getTotalHits()+"",collect1);

    }


    /**
     * Es增量添加
     * @param fangyuanEsDto
     * @return AjaxResult
     */
    @Override
    public boolean fangYuanEsSave(FangyuanEsDto fangyuanEsDto) {
        try {
            fangyuanEsMapper.save(fangyuanEsDto);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }



}
